package io.lckgroup.modules.lck.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.exceptions.RequestException;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.NotificationTemplate;
import com.gexin.rp.sdk.template.style.Style0;
import io.lckgroup.modules.lck.utils.UniPushUtils;
import org.json.JSONObject;
import io.lckgroup.modules.lck.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.lckgroup.common.utils.PageUtils;
import io.lckgroup.common.utils.Query;

import io.lckgroup.modules.lck.dao.CommonFeedbackDao;
import io.lckgroup.modules.lck.service.CommonFeedbackService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;


@Service("commonFeedbackService")
public class CommonFeedbackServiceImpl extends ServiceImpl<CommonFeedbackDao, CommonFeedbackEntity> implements CommonFeedbackService {


    @Autowired
    private CommonFeedbackDao commonFeedbackDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CommonFeedbackEntity> page = this.page(
                new Query<CommonFeedbackEntity>().getPage(params),
                new QueryWrapper<CommonFeedbackEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * @param params
     * @return 商家反馈信息
     */
    @Override
    @Cacheable(value = "selectCommonFeedback", key = "#params.get(\"redis\")") //存方法返回值
    public IPage<Map<String, Object>> selectCommonFeedback(Map<String, Object> params) {
        String merchantName = params.get("key").toString();
        ShopMerchantInfoEntity shopMerchantInfoEntity = new ShopMerchantInfoEntity();
        if (merchantName.equals("")) {
            shopMerchantInfoEntity.setMerchantName(null);
        } else {
            shopMerchantInfoEntity.setMerchantName(merchantName);
        }
        Page<Map<String, Object>> page = new Page<>(Integer.parseInt(params.get("page").toString()), Integer.valueOf(params.get("limit").toString()));
        return commonFeedbackDao.selectCommonFeedback(page, shopMerchantInfoEntity.getMerchantName());
    }

    @Override
    /**
     * 处理 批处理反馈
     */
    @Transactional
    @CacheEvict(value = {"selectCommonFeedback"}, allEntries = true)
    public void updateBatchById(@RequestBody Long[] ids) {
        ArrayList<CommonFeedbackEntity> commonList = new ArrayList<>();
        for (int i = 0; i < ids.length; i++) {
            CommonFeedbackEntity commonFeedbackEntity = new CommonFeedbackEntity();
            commonFeedbackEntity.setIsHandle(2);
            commonFeedbackEntity.setId(ids[i]);
            commonList.add(commonFeedbackEntity);
        }
        /**
         * 二期 告知商家 反馈已经处理
         */
        List<Map<String,Object>> map = commonFeedbackDao.selectCommonFeedbackAndMerchantInfo(ids);

        //处理反馈
        updateBatchById(commonList);

        for (int i = 0; i <map.size() ; i++) {
            UniPushUtils.pushDealFeedBack(map.get(i).get("client_id").toString());
//            tuisong(map.get(i).get("client_id").toString(),map.get(i).get("feedback_info").toString());
        }

    }

    @Override
    public PageUtils queryPageByEntity(CommonFeedbackEntity entity) {

        IPage<CommonFeedbackEntity> page = this.page(
                new Query<CommonFeedbackEntity>().getPage(new HashMap<String, Object>()),
                new QueryWrapper<CommonFeedbackEntity>().setEntity(entity)
        );

        return new PageUtils(page);
    }

}
